package Coffee.service;

import Coffee.bean.Coffee;
import Coffee.bean.Order;
import Coffee.bean.Result;
import Coffee.bean.User;
import Coffee.dao.OrderDao;
import Coffee.dao.OrderDaoImpl;
import Coffee.util.Cbase;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

public class OrderServiceImpl extends Cbase implements OrderService {
    UserService userService = new UserServiceImpl();
    CoffeeService coffeeService = new CoffeeServiceImpl();
    OrderDao orderDao = new OrderDaoImpl();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    String formattedDate = LocalDateTime.now().format(formatter);

    @Override
    public Result insertOrder(String username, String cofname, int cofcount) {
        Result result = new Result();
        //传入的是用户名，coffee名，数量，先查询用户名判断是否存在，然后咖啡名是否存在，返回id和价格
        User user = userService.queryUser(username);
        if (user == null) {
            result.setSuccess(false);
            result.setMessage("没登陆");//防自己没设置拦截
            return result;
        }
        Coffee coffee = coffeeService.selectbyname(cofname);
        int coftotal = cofcount * coffee.getCofprice();
            int i = orderDao.insertOrder(UUID.randomUUID().toString().replaceAll("-", ""), user.getUserid(),
                    username, coffee.getCofid(), cofname, cofcount, coftotal, formattedDate);
            if(i == 1) {
                result.setSuccess(true);
            } else {
                result.setSuccess(false);
                result.setMessage("下单失败");
            }
        return result;
    }

    @Override
    public List<Order> queryOrder(String username) {
        return orderDao.queryOrder(username);

    }

    @Override
    public List<Order> queryOrderAll() {
        return orderDao.queryOrderAll();
    }

    @Override
    public List<Order> getAll(String username, String state,int pageIndex, int pageSize) {
        return orderDao.getAll(username,state,pageIndex,pageSize);
    }

    @Override
    public int getCount(String username, String state) {
        return orderDao.getCount(username,state);
    }

    @Override
    public void updateOrderState(String orderstate, String list) {
        int i = orderDao.updateOrderState(orderstate, list);
        boolean od = (i!=0);
        System.out.println(od?"修改成功":"修改失败");
    }

    @Override
    public Order queryOrderByOrderId(String orderid) {
        return orderDao.queryOrderByOrderId(orderid);
    }

    @Override
    public List<Order> queryOrderAdmin(String username) {
        return orderDao.queryOrderAdmin(username);
    }

    @Override
    public Result del(String[] list) {
        Result result = new Result();
        System.out.println("service");
        int del = orderDao.del(list);
        if(del>0){
            result.setMessage("删除成功");
            result.setSuccess(true);
            return result;
        }
        result.setMessage("F12");
        result.setSuccess(false);
        return result;
    }

}

